package com.bw.property.adapter

import android.content.res.ColorStateList
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.bw.common.adapter.CommonBindingAdapter
import com.bw.property.R
import com.bw.property.databinding.ItemParkBinding
import com.bw.property.entity.ParkValue

/**
 * 车位列表数据适配器
 */
class ParkListAdapter constructor(private val list:MutableList<ParkValue>):RecyclerView.Adapter<ParkListAdapter.ItemViewHolder>() {

    private var mListener:ItemParkOnClickListener?=null

    inner class ItemViewHolder( val itemDataBinding: ItemParkBinding):RecyclerView.ViewHolder(itemDataBinding.root)


    interface ItemParkOnClickListener{
        fun onClick(v:View,position: Int)
    }
    fun setOnItemClickListener(listener:ItemParkOnClickListener){
        this.mListener=listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemDataBinding = DataBindingUtil.inflate<ItemParkBinding>(LayoutInflater.from(parent.context),R.layout.item_park,parent,false)
        return ItemViewHolder(itemDataBinding)
    }

    override fun getItemCount(): Int =list.size
    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        var valueEntity = list[position]
        holder.itemDataBinding.tvParkName.text = valueEntity.parkName
        //设置不同颜色
        when(valueEntity.parkUser){
            -1->{
                holder.itemDataBinding.tvParkName.setTextColor(Color.WHITE)
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.WHITE)
                holder.itemDataBinding.tvParkName.text = valueEntity.parkName
            }
            0->{
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.parseColor("#8fdb0a"))
                holder.itemDataBinding.tvParkName.text = "空位"
            }
            else->{
                holder.itemDataBinding.tvParkName.text = valueEntity.parkName
                holder.itemDataBinding.tvParkName.setBackgroundColor(Color.parseColor("#CCCCCC"))
            }
        }

        //添加点击事件
        holder.itemDataBinding.tvParkName.setOnClickListener {
            if(valueEntity.parkUser !=-1){
                mListener?.onClick(it,position)
            }
        }
    }


}
